package com.example.yunJie.digitalSentry.control;


import com.example.yunJie.digitalSentry.entity.*;
import com.example.yunJie.digitalSentry.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/ShiftDate")
public class ShiftDateControl {

    @Autowired
    private ShiftService shiftService;

    @Autowired
    private PeriodService periodService;
    @Autowired
    private ShiftDateService shiftDateService;
    @Autowired
    private DateService dateService;
    @Autowired
    private ShiftPeriodService shiftPeriodService;

    @PostMapping("add")
    public String insert (@RequestBody List<ShiftDateEntity> shiftDateEntities){
        String s = "";
        int count  = 0;


        for (ShiftDateEntity shiftDateEntity : shiftDateEntities){

            DateEntity dateEntity1 = new DateEntity();
            dateEntity1.setDate(shiftDateEntity.getDate());
            DateEntity byDate = dateService.findByDate(shiftDateEntity.getDate());
            if (byDate == null){
                dateService.insert(dateEntity1);
            }

        }

        for (ShiftDateEntity shiftDateEntity : shiftDateEntities){
            ShiftDateEntity shiftDateEntity1 = new ShiftDateEntity();
            shiftDateEntity1.setDateId(String.valueOf(dateService.findByDate(shiftDateEntity.getDate()).getId()));
            shiftDateEntity1.setShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(),shiftDateEntity.getPostName(),shiftDateEntity.getShiftName()).getId()));

            ShiftPeriodEntity shiftPeriodEntity1 = new ShiftPeriodEntity();
            shiftPeriodEntity1.setShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(),shiftDateEntity.getPostName(),shiftDateEntity.getShiftName()).getId()));
            shiftPeriodEntity1.setPeriodId(String.valueOf(periodService.findByMode(shiftDateEntity.getMode()).getId()));
            ShiftPeriodEntity shiftPeriodEntity = shiftPeriodService.find(shiftPeriodEntity1);
            if (shiftPeriodEntity != null){
                PeriodEntity periodEntity = periodService.findId(shiftPeriodService.findByShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(), shiftDateEntity.getPostName(), shiftDateEntity.getShiftName()).getId())).getPeriodId());
                if (shiftDateEntity.getMode().equals(periodEntity.getMode())){
                    if (shiftDateService.find(shiftDateEntity1) == null){
                        shiftDateService.insert(shiftDateEntity1);
                        count++;
                    }
                }
//                if (count >0){
//                    s = "操作成功";
//                }else {
//                    s = "该班次已有模式";
//                }

            }else {
                shiftPeriodService.insert(shiftPeriodEntity1);
                if (shiftDateService.find(shiftDateEntity1) == null){
                    shiftDateService.insert(shiftDateEntity1);
                    s = "操作成功";
                }
            }

        }

        return s;

    }

    @RequestMapping("findAll")
    public List<ShiftDateEntity> findAll(ShiftDateEntity shiftDateEntity){
        List<ShiftDateEntity> newall = new ArrayList<>();
        List<ShiftDateEntity> all = shiftDateService.findAll(shiftDateEntity);
        for (ShiftDateEntity shiftDateEntity1 : all){
            if (shiftService.findId(String.valueOf(shiftDateEntity1.getId())) != null){
                newall.add(shiftDateEntity1);
            }
        }

        for (ShiftDateEntity shiftDateEntity1 : newall){
            System.out.println(shiftDateEntity1);
            List<String> ls = new ArrayList<>();
            String attendanceDetails = shiftDateEntity1.getAttendanceDetails();
            if (attendanceDetails.contains("星期")){
                String[] split = attendanceDetails.split(",");
                String[] newSplit = new String[7];
                System.out.println(split.toString());
                for (String s:split){
                    if (s.equals("星期一")){
                        newSplit[0] = s;
                    }else if (s.equals("星期二")){
                        newSplit[1] = s;
                    }else if (s.equals("星期三")){
                        newSplit[2] = s;
                    }else if (s.equals("星期四")){
                        newSplit[3] = s;
                    }else if (s.equals("星期五")){
                        newSplit[4] = s;
                    }else if (s.equals("星期六")){
                        newSplit[5] = s;
                    }else if (s.equals("星期日")){
                        newSplit[6] = s;
                    }
                }
                for (String s:newSplit){
                    System.out.println(s);
                }
                String s = Arrays.toString(newSplit);
                String s1 = s.replace(", null"," ");
                String s2 = s1.substring(1,s1.length()-1);
                String s3 = s2.replace("null"," ");
                shiftDateEntity1.setAttendanceDetails(s3.toString());

            }else {
                List<String> list = new ArrayList();
                String[] split = attendanceDetails.split(",");
                for (String s: split){
                    list.add(s);
                }
                Collections.sort(list);
                for (String s:list){
                    System.out.println(s);
                }
                String s = list.toString();
                String s1 = s.substring(1,s.length()-1);
                shiftDateEntity1.setAttendanceDetails(s1);


            }

        }



        return newall;
    }

    @RequestMapping("/update")
    public String update(@RequestBody List<ShiftDateEntity> shiftDateEntities){
        String s = "";

        for(ShiftDateEntity shiftDateEntity : shiftDateEntities){
            shiftDateService.deleteByShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(),shiftDateEntity.getPostName(),shiftDateEntity.getShiftName()).getId()));
        }

        for (ShiftDateEntity shiftDateEntity : shiftDateEntities){
            DateEntity dateEntity1 = new DateEntity();
            dateEntity1.setDate(shiftDateEntity.getDate());
            DateEntity byDate = dateService.findByDate(shiftDateEntity.getDate());
            if (byDate == null){
                dateService.insert(dateEntity1);
            }
        }

        for (ShiftDateEntity shiftDateEntity : shiftDateEntities){
            ShiftDateEntity shiftDateEntity1 = new ShiftDateEntity();
            shiftDateEntity1.setDateId(String.valueOf(dateService.findByDate(shiftDateEntity.getDate()).getId()));
            shiftDateEntity1.setShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(),shiftDateEntity.getPostName(),shiftDateEntity.getShiftName()).getId()));

            ShiftPeriodEntity shiftPeriodEntity1 = new ShiftPeriodEntity();
            shiftPeriodEntity1.setShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(),shiftDateEntity.getPostName(),shiftDateEntity.getShiftName()).getId()));
            shiftPeriodEntity1.setPeriodId(String.valueOf(periodService.findByMode(shiftDateEntity.getMode()).getId()));
            ShiftPeriodEntity shiftPeriodEntity = shiftPeriodService.find(shiftPeriodEntity1);
            if (shiftPeriodEntity != null){
//                s="该班次已有模式";
                PeriodEntity periodEntity = periodService.findId(shiftPeriodService.findByShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(), shiftDateEntity.getPostName(), shiftDateEntity.getShiftName()).getId())).getPeriodId());
                if (shiftDateEntity.getMode().equals(periodEntity.getMode())){
                    if (shiftDateService.find(shiftDateEntity1) == null){
                        shiftDateService.insert(shiftDateEntity1);
                    }
                }
            }else {
                shiftDateService.deleteByShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(), shiftDateEntity.getPostName(), shiftDateEntity.getShiftName()).getId()));
                shiftPeriodService.deleteByShiftId(String.valueOf(shiftService.findBy(shiftDateEntity.getMarketName(), shiftDateEntity.getPostName(), shiftDateEntity.getShiftName()).getId()));
                shiftPeriodService.insert(shiftPeriodEntity1);
                if (shiftDateService.find(shiftDateEntity1) == null){
                    shiftDateService.insert(shiftDateEntity1);
                    s = "操作成功";
                }
            }
        }

        return s;
    }


    @RequestMapping("delete")
    //根据shiftId删除中间表的所有该shiftI的数据
    public int delete(@RequestBody List<ShiftDateEntity> shiftDateEntities){
        int i =0;
        for (ShiftDateEntity shiftDateEntity :shiftDateEntities){
            List<ShiftEntity> allByMarketNameAndPostNameAndShiftName = shiftService.findAllByMarketNameAndPostNameAndShiftName(shiftDateEntity.getMarketName(), shiftDateEntity.getPostName(), shiftDateEntity.getShiftName());
            shiftDateService.deleteByShiftId(String.valueOf(allByMarketNameAndPostNameAndShiftName.get(0).getId()));
            shiftPeriodService.deleteByShiftId(String.valueOf(allByMarketNameAndPostNameAndShiftName.get(0).getId()));

        }
        List<ShiftEntity> allByMarketNameAndPostNameAndShiftName = shiftService.findAllByMarketNameAndPostNameAndShiftName(shiftDateEntities.get(0).getMarketName(), shiftDateEntities.get(0).getPostName(), shiftDateEntities.get(0).getShiftName());
        ShiftDateEntity shiftDateEntity = new ShiftDateEntity();
        shiftDateEntity.setShiftId(String.valueOf(allByMarketNameAndPostNameAndShiftName.get(0).getId()));
        ShiftDateEntity shiftDateEntity1 = shiftDateService.find(shiftDateEntity);
        if (shiftDateEntity1 != null)   {
            return i =0;
        }else {
            return i=1;
        }
    }

    @RequestMapping("deleteList")
    public int deleteList(@RequestBody List<ShiftDateEntity> shiftDateEntities){
        int i =0;
        for (ShiftDateEntity shiftDateEntity :shiftDateEntities){
            List<ShiftEntity> allByMarketNameAndPostNameAndShiftName = shiftService.findAllByMarketNameAndPostNameAndShiftName(shiftDateEntity.getMarketName(), shiftDateEntity.getPostName(), shiftDateEntity.getShiftName());
            shiftDateService.deleteByShiftId(String.valueOf(allByMarketNameAndPostNameAndShiftName.get(0).getId()));
            shiftPeriodService.deleteByShiftId(String.valueOf(allByMarketNameAndPostNameAndShiftName.get(0).getId()));

        }
        List<ShiftEntity> allByMarketNameAndPostNameAndShiftName = shiftService.findAllByMarketNameAndPostNameAndShiftName(shiftDateEntities.get(0).getMarketName(), shiftDateEntities.get(0).getPostName(), shiftDateEntities.get(0).getShiftName());
        ShiftDateEntity shiftDateEntity = new ShiftDateEntity();
        shiftDateEntity.setShiftId(String.valueOf(allByMarketNameAndPostNameAndShiftName.get(0).getId()));
        ShiftDateEntity shiftDateEntity1 = shiftDateService.find(shiftDateEntity);
        if (shiftDateEntity1 != null)   {
            return i =0;
        }else {
            return i=1;
        }

    }

}
